from django.shortcuts import render
from .models import *
from django.core.paginator import Paginator,Page
from df_cart.models import CartInfo
# Create your views here.

def index(request):
    #登录后要在首页显示购物车数量
    # count = CartInfo.objects.get(user_id=request.session.get('user_id')).count()
    count = CartInfo.objects.filter(user_id=request.session.get('user_id')).count()
    #查询各分类的最新四条、最热四条数据
    typelist = TypeInfo.objects.all()
    type0 = typelist[0].goodsinfo_set.order_by('-id')[0:4]
    type01 = typelist[0].goodsinfo_set.order_by('-gclick')[0:4]
    type1= typelist[1].goodsinfo_set.order_by('-id')[0:4]
    type11 = typelist[1].goodsinfo_set.order_by('-gclick')[0:4]
    type2 = typelist[2].goodsinfo_set.order_by('-id')[0:4]
    type21 = typelist[2].goodsinfo_set.order_by('-gclick')[0:4]
    type3 = typelist[3].goodsinfo_set.order_by('-id')[0:4]
    type31 = typelist[3].goodsinfo_set.order_by('-gclick')[0:4]
    type4 = typelist[4].goodsinfo_set.order_by('-id')[0:4]
    type41 = typelist[4].goodsinfo_set.order_by('-gclick')[0:4]
    type5 = typelist[5].goodsinfo_set.order_by('-id')[0:4]
    type51 = typelist[5].goodsinfo_set.order_by('-gclick')[0:4]
    context={
        'title':'首页', 'guest_cart':1,
        'type0':type0, 'typ01':type01,
        'type1': type1, 'typ01': type11,
        'type2': type2, 'typ01': type21,
        'type3': type3, 'typ01': type31,
        'type4': type4, 'typ01': type41,
        'type5': type5, 'typ01': type51,
        'count': count,
    }
    return render(request, 'df_goods/index.html' , context)


def list(request, typeid, pageid, sort):
    """
       goodlist函数负责展示某类商品的信息。
       url中的参数依次代表
       typeid:商品类型id;selectid:查询条件id，1为根据id查询，2位根据价格查询，3位根据点击量查询
       """
    count = request.session.get('count')
    # 获取最新发布的商品
    newgoods = GoodsInfo.objects.all().order_by('-id')[0:2]
    # print(newgoods)
    # 根据条件查询所有商品
    if sort == '1':  # 按最新   gtype_id  , gtype__id  指typeinfo_id
        sumGoodList = GoodsInfo.objects.filter(
            gtype_id=typeid).order_by('-id')
    elif sort == '2':  # 按价格
        sumGoodList = GoodsInfo.objects.filter(
            gtype__id=typeid).order_by('gprice')
    elif sort == '3':  # 按点击量
        sumGoodList = GoodsInfo.objects.filter(
            gtype__id=typeid).order_by('-gclick')
    # 分页
    paginator = Paginator(sumGoodList, 15)
    goodList = paginator.page(int(pageid))
    pindexlist = paginator.page_range
    # print pindexlist    xrange(1,2)
    # 确定商品的类型
    goodtype = TypeInfo.objects.get(id=typeid)
    count = CartInfo.objects.filter(user_id=request.session.get('user_id')).count()
    # 构造上下文  'count': count,
    context = {'title': '商品详情', 'list': 1,
               'guest_cart': 1, 'goodtype': goodtype,
               'newgoods': newgoods, 'goodList': goodList,
               'typeid': typeid, 'sort': sort,
               'pindexlist': pindexlist, 'pageid': int(pageid), 'count': count}

    # 渲染返回结果
    return render(request, 'df_goods/list.html', context)

#详情
def detail(request, id):
    goods = GoodsInfo.objects.get(id=id)
    #每点进去一下，点击量加一
    goods.gclick=goods.gclick+1
    goods.save()

    #显示购物车数量
    count = CartInfo.objects.filter(user_id=request.session.get('user_id')).count()
    newgoods = goods.gtype.goodsinfo_set.order_by('-id')[0:2] #新品推荐
    context = {
        'title':goods.gtype.ttitle,
        'guest_cart':1,
        'g':goods,
        'newgoods':newgoods,
        'id':id,
        'count':count,
    }
    response = render(request, 'df_goods/detail.html', context)
    #记录最近浏览，在用户中心使用
    goods_ids = request.COOKIES.get('goods_ids', '')
    goods_id = str(goods.id)
    if goods_ids != '' : #判断是否有浏览记录，如果有则继续判断
        goods_ids1 = goods_ids.split(',')#拆分为列表
        if goods_ids1.count(goods_id)>=1:#如果商品已经被记录，则删除
            goods_ids1.remove(goods_id)
        goods_ids1.insert(0,goods_id) #添加到第一个
        if len(goods_ids1)>=6 : #如果超过6个则删除最后一个
            del goods_ids1[5]
        goods_ids=','.join(goods_ids1)#拼接为字符串
    else:
        goods_ids = goods_id #如果没有则直接添加

    response.set_cookie('goods_ids',goods_ids)#写入cookie
    return response



#抽奖
def choujiang(request):
    return render(request, 'df_goods/choujiang.html')









